package com.tmc.web.controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tmc.util.ObjectUtil;
import com.tmc.web.BaseController;

@Controller
@RequestMapping("/download")
public class FileController extends BaseController {

	private Log logger = LogFactory.getLog(this.getClass());
	
	@Value("${create_path}")
	private String create_path;
	
	@RequestMapping(value = "/create{fileName}", produces = { "application/vnd.ms-excel;charset=UTF-8" })
	public void fileDownload(String fileName, HttpServletResponse response) {
		try {
			if (ObjectUtil.noText(fileName)) {
				return;
			}
			response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
			InputStream in = new FileInputStream(create_path + fileName);
			int len = 0;
			byte[] buffer = new byte[1024];
			OutputStream out = response.getOutputStream();
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
			in.close();
		} catch (Exception e) {
			logger.error("导出excel出错！", e);
		}
	}

}
